<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    async function randomDelay (id) {
      const delay = Math.random() * 10000;
      return new Promise((resolve) => setTimeout(() => {
            console.log(`${id} finished`);
            resolve(id);
      }, delay))
    }
    const result = []
    async function foo () {
      const t0 = Date.now()
      const promises = Array(5).fill(null).map((_, i) => randomDelay(i))
      for (const p of promises) {
        result.push(await p)
      }
      console.log(result)
      console.log(`${Date.now() - t0}ms elapsed`)
    }
    foo()
  </script>
</body>

</html>
